programming4us
           
 
 
Programming

jQuery 1.3 : Improving a basic form (part 3) - Required field messages

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/6/2010 5:47:08 PM
Required field messages

In our contact form, required fields have class="required" to allow for styling as well as response to user input; the input fields for each type of contact have class="conditional" applied to them. We're going to use these classes to change the instructions printed within parentheses to the right of each input.

We start by setting variables for requiredFlag and conditionalFlag, and then we fill the<span> element next to each required and conditional field with the text stored in those variables:

$(document).ready(function() {
var requiredFlag = ' * ';
var conditionalFlag = ' ** ';
$('form :input')
.filter('.required')
.next('span').text(requiredFlag).end()
.end()
.filter('.conditional')
.next('span').text(conditionalFlag);
});

Using .end() allows us to extend the chain of methods so that we continue to work with the same set of elements and keep object creation and DOM traversal to a minimum. Each .end() method takes the selection back one step, reverting the matched set of elements to what it was before the last traversal method. Here we use two in a row: the first .end() reverts the matched set to .filter('.required') and the second reverts it to $('form :input'). Thus, when .filter('.conditional') selects elements with class="conditional", it applies to all inputs within the form.

Now, since a single asterisk (*) may not immediately capture the user's attention, we'll also add class="req-label" to the<label> for each required field and apply font-weight:bold to that class. To do so, we can extend the chain even further.

$(document).ready(function() {
var requiredFlag = ' * ';
var conditionalFlag = ' ** ';
$('form :input')
.filter('.required')
.next('span').text(requiredFlag).end()
.prev('label').addClass('req-label').end()
.end()
.filter('.conditional')
.next('span').text(conditionalFlag);
});

Such a long chain of methods can be difficult to follow, so a consistent line-break and indentation pattern is essential.

The fieldset with the modified text and the added class now looks like this:

Not bad. Still, the required and conditional field messages really weren't so bad after all; they were just too repetitive. Let's take the first instance of each message and display it above the form next to the flag we're using to symbolize it.

Before we populate the<span> elements holding the messages with their respective flags, we need to store the initial messages in a couple of variables. Then we can strip out the parentheses by using a regular expression:

$(document).ready(function() {
var requiredFlag = ' * ';
var conditionalFlag = ' ** ';
var requiredKey = $('input.required:first')
.next('span').text();
var conditionalKey = $('input.conditional:first')
.next('span').text();
requiredKey = requiredFlag +
requiredKey.replace(/^\((.+)\)$/,'$1');
conditionalKey = conditionalFlag +
conditionalKey.replace(/^\((.+)\)$/,'$1');
// . . . code continues
});

The first two additional lines declare variables&mdash;requiredKey and conditionalKey&mdash;to store each field type's text. The second two lines modify the text in those variables, concatenating each flag, and its respective text, minus the parentheses. Perhaps the regular expression, along with its .replace() method, warrants further explanation.

Other -----------------
- Changes to Privacy Risk Management and Compliance in Relation to Cloud Computing
- Cloud Security and Privacy : What Are the Key Privacy Concerns in the Cloud?
- Cloud Security and Privacy : What Is the Data Life Cycle?
- Making Your Site Accessible to Search Engines
- Security Management in the Cloud - Security Vulnerability, Patch, and Configuration Management (part 2)
- Security Management in the Cloud - Security Vulnerability, Patch, and Configuration Management (part 1)
- Security Management in the Cloud - Access Control
- Security Management in the Cloud - IaaS Availability Management
- Security Management in the Cloud - PaaS Availability Management
- Security Management in the Cloud - SaaS Availability Management
- Security Management in the Cloud - Availability Management
- Security Management in the Cloud
- The Art of SEO : Trending, Seasonality, and Seasonal Fluctuations in Keyword Demand
- The Art of SEO : Leveraging the Long Tail of Keyword Demand
- The Art of SEO : Determining Keyword Value/Potential ROI
- Identity and Access Management : Cloud Service Provider IAM Practice
- Identity and Access Management : Cloud Authorization Management
- Identity and Access Management : IAM Practices in the Cloud (part 2) - Federated Identity
- Identity and Access Management : IAM Practices in the Cloud (part 1) - Cloud Identity Administration
- iPad SDK : Keyboard Extensions and Replacements (part 4) - Creating the Calculator
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us